/* Most of this code comes directly from Autor, Houseman, and Kerr */
/* https://www.journals.uchicago.edu/doi/suppl/10.1086/687522 */

#delimit ;
set more off;
set mat 11000;
macro drop _all;

capture log close;

do gqrcode_ahk;
/*OLS and IV Estimates of the Effect of Work First job placements on earnings and employment quarters 2-8;
*/;

clear;
gzuse ddata_v9;

gen distyr = distr*10 + partyr;
gen contyr = 10*contr + partyr;

gen regern2_8=(regern2_4+regern5_8);
gen tmpern2_8=(tmpern2_4+tmpern5_8);

quietly {;
  foreach v in wageadj2_4 regern2_4 tmpern2_4  {;
    replace `v'=`v'/3;
  };
   foreach v in wageadj5_8 regern5_8 tmpern5_8  {;
    replace `v'=`v'/4;
  };
   foreach v in wageadj2_8 regern2_8 tmpern2_8 js7_tern new_ear2_8 {;
    replace `v'=`v'/7;
  };
   foreach v in wageadjh1_8 qtremph1_8 qtrtemph1_8 tmpernh1_8 {;
    replace `v'=`v'/8;
  };
};

recode js7_tern .=0;
recode new_ear2_8 .=0;

* Drop the contractor-year cells where tercile equality dropped;

drop if contyr==173|contyr==323|contyr==180|contyr==143|contyr==163|contyr==251|contyr==282|contyr==220|contyr==330|contyr==131|contyr==312|contyr==193|contyr==211;
drop if contyr==60|contyr==23|contyr==92|contyr==13|contyr==40|contyr==51|contyr==100;



* Table 2;
sort wageadj2_8;
gen n=_n;
gen double centile=100*n/30522;
replace centile=round(centile,1)/100;
sum centile;
gen pct_direct=regern2_8/wageadj2_8;
foreach c in 0.15 0.25 0.50 0.75 0.85 {;
sum wageadj2_8 pct_direct if centile==`c';
};

drop n centile pct_direct;



xi i.contyr i.distyr;



local olsvar age age2 female wh_race oth_race wageadjh1_8 qtremph1_8 yq20001 yq20002 yq20003 yq20004 
             yq20011 yq20012 yq20013 yq20014 yq20021 yq20022 yq20023 yq20024 yq20031;

/* demean all OLS RHS variables */;

* 3-Way;

areg tempemp `olsvar' _Idistyr*, absorb(contyr);
predict ert, dresid;
egen meanert = mean(ert), by(contyr);
rename meanert temp_pct;

areg regemp `olsvar' _Idistyr*, absorb(contyr);
predict err, dresid;
egen meanerr = mean(err), by(contyr);
rename meanerr reg_pct;

areg emp `olsvar' _Idistyr*, absorb(contyr);
predict eer, dresid;
egen meaneer = mean(eer), by(contyr);
rename meaneer emp_pct;


_rmcoll `olsvar' _Idistyr* ;
local cont `r(varlist)';



drop _Idistyr_121 _Idistyr_122 _Idistyr_123;


/* Start of New Code */

for num 1/2: gen bX=.;
for num 1/2: gen hX=.;
for num 1/2: gen lX=.;

foreach num of numlist 30(10)90 {;
	quan wageadj2_8, end1(tempemp) end2(regemp) prone(`olsvar' _Idistyr*) instr(temp_pct reg_pct) tau(`num') ;
	for num 1/2: replace bX=e(bX) if nnn==`num';
	for num 1/2: replace hX=e(highX) if nnn==`num';
	for num 1/2: replace lX=e(lowX) if nnn==`num';
	ereturn list;
	
	preserve;
	keep if nnn==`num';
	keep b1 b2 l1 l2 h1 h2 nnn;

	if (`num'==30) {;
		save results, replace;
	};
	else {;
		append using results;
		save results, replace;
	};
	restore;
	
};







